<?php
App::uses('AppController', 'Controller');
/**
 * Users Controller
 *
 * @property User $User
 */
class UsersController extends AppController {


	public function troca_senha(){
		if(!empty($this->request->data)){
			if($this->request->data['User']['id_users'] == $this->usuarioAtual['id_users']){
				if($this->User->senhaCorreta($this->request->data['User']['id_users'], $this->request->data['User']['senha_atual']))
					if($this->User->save($this->request->data)){
						$this->Session->setFlash(__('Senha alterada com sucesso!'));
					}
			}
			else{
				$this->Session->setFlash(__('Seu usuário é inválido'));
			}
		}
	}
	
	public function emails($todos = null){
		if(!is_null($todos)){
			$this->User->id = $this->usuarioAtual['id_users'];
			$this->User->saveField('receber_email', !$this->User->field('receber_email'));
			$this->usuarioAtual['receber_email'] = $this->User->field('receber_email');
			$id = $this->User->id;
			$this->request->data['User'] = array_merge($this->usuarioAtual, array('id_users' => $id));
			$this->Auth->login($this->request->data['User']);
			$this->redirect('emails');
		}
		$this->set('user', $this->usuarioAtual);
	}
	
	public function admin_troca_senha() {
		$this->render('troca_senha');
		$this->troca_senha();
	}

	
	public function admin_index() {
		if(!$this->isAdmin()) return false;
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());
	}

/**
 * view method
 *
 * @param string $id
 * @return void
 */
	public function admin_view($id = null) {
		if(!$this->isAdmin()) return false;
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Usuário inválido'));
		}
		$this->set('user', $this->User->read(null, $id));
	}

/**
 * add method
 *
 * @return void
 */
	public function admin_add() {
		if(!$this->isAdmin()) return false;
		if ($this->request->is('post')) {
			$this->User->create();
			if ($this->User->save($this->request->data)) {
				$this->Session->setFlash(__('The user has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
			}
		}
		$empresas = $this->User->Empresa->find('list');
		$projetos = $this->User->Projeto->find('list');
		$this->set(compact('empresas', 'projetos'));
	}

/**
 * edit method
 *
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
		if(!$this->isAdmin()) return false;
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->User->save($this->request->data)) {
				$this->Session->setFlash(__('The user has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->User->read(null, $id);
		}
		$empresas = $this->User->Empresa->find('list');
		$projetos = $this->User->Projeto->find('list');
		$this->set(compact('empresas', 'projetos'));
	}

/**
 * delete method
 *
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		if(!$this->isAdmin()) return false;
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		if ($this->User->delete()) {
			$this->Session->setFlash(__('User deleted'));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('User was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
	
	public function admin_login(){
		$this->login();
		$this->render('login');
	}
	
	public function admin_logout(){
		$this->logout();
	}
	
	
	// TODO Manter na página que tentou entrar
	public function login() {
	    if ($this->request->is('post')) {
	        if ($this->Auth->login()) {
	        		$usuario = $this->Auth->user();
	        		if($usuario['admin'])
	            		return $this->redirect(array('controller'=>'projetos', 'action'=>'index', 'admin'=>true));
	            	else 
	            		return $this->redirect($this->Auth->redirect());
	        } else {
	            $this->Session->setFlash("Login inválido!", 'default', array(), 'auth');
	        }
	    }
	}
	
	public function logout(){
		$this->redirect($this->Auth->logout());
	}
}
